using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class BrowseEditWindow : MonoBehaviour {
	
	public GameObject EntryPrefab;
	
	public UILabel Title;
	public UILabel Filter;
	public GameObject FilterButton;
	public GameObject NewButton;
	public GameObject OKButton;
	public UITable entryHolder;
	
	public string infoButtonOverrideFunction;
	public string entryToggledFunction;
	public string newClickedFunction;
	public string okClickedFunction;
	public GameObject target;
	
	public List<string> toggledEntries;
	public List<GameObject> entryObjects; 
	
	public string infoTarget;
	
	public GameObject origin;
	
	public bool singleSelect = false;
	
	HelperFunctions.ObjectType type;
	
	// Use this for initialization
	void Start () {
		if(toggledEntries == null)
			toggledEntries = new List<string>();
		
		UIButtonMessage message = FilterButton.AddComponent<UIButtonMessage>();
		message.target = gameObject;
		message.functionName = "FilterList";
		
		message = OKButton.AddComponent<UIButtonMessage>();
		message.target = gameObject;
		message.functionName = "OKButtonClicked";
		
		message = NewButton.AddComponent<UIButtonMessage>();
		message.target = gameObject;
		message.functionName = "NewButtonClicked";
	}
	
	// Update is called once per frame
	void Update () {
		
	}
	
	public void Setup(string browsedType, Dictionary<string,string> entries, List<string> currentSelections, HelperFunctions.ObjectType type, bool disableInfo = false, bool singleSelect = false)
	{
		this.type = type;
		
		Title.text = "Browse " + browsedType;
		
		foreach(string entry in entries.Keys)
		{	
			GameObject g = Instantiate(EntryPrefab) as GameObject;
			g.transform.parent = entryHolder.transform;
			g.transform.localScale = Vector3.one;
			g.transform.localPosition = Vector3.zero;
			g.name = entry;
			
			BrowseEditEntry bee = g.GetComponent<BrowseEditEntry>();
			bee.callBackTarget = gameObject;
			bee.Setup(entry, type, disableInfo);
			if(currentSelections != null && currentSelections.Contains(bee.entryFile))
				bee.setupClick();
			
			entryObjects.Add(g);
		}
		
		toggledEntries = currentSelections;

		if(singleSelect)
		{
			this.singleSelect = true;
			foreach(GameObject bee in entryObjects)
			{
				foreach(GameObject bee2 in entryObjects)
				{
					if(bee2 != bee)
					{
						UIButtonTween t = bee.gameObject.AddComponent<UIButtonTween>();
						t.tweenTarget = bee2;
						t.playDirection = AnimationOrTween.Direction.Reverse;
					}
				}
			}
		}
		
		if(entryObjects.Count > 1)
		{
			Destroy(EntryPrefab);
			EntryPrefab = entryObjects[0];
		}
		else
		{
			EntryPrefab.SetActiveRecursively(false);
		}
		
		entryHolder.repositionNow = true;
	}
	
	public void ReturnMessageNew(KeyValuePair<string,string> entry)
	{
		GameObject g = Instantiate(EntryPrefab) as GameObject;
		g.transform.parent = entryHolder.transform;
		g.transform.localScale = Vector3.one;
		g.transform.localPosition = Vector3.zero;
		g.name = entry.Key;
		
		BrowseEditEntry bee = g.GetComponent<BrowseEditEntry>();
		bee.callBackTarget = gameObject;
		bee.Setup(entry.Key, type, false);
		
		entryObjects.Add(g);
		
		Filter.text = "";
		FilterList();
	}
	
	public void FilterList()
	{
		foreach(GameObject g in entryObjects)
		{
			g.SetActiveRecursively(true);
		}
		
		List<GameObject> nonMatched = entryObjects.FindAll( 
										delegate(GameObject o) {
											return Filter.text != "Enter Search Filter" && !o.name.ToUpper().Contains(Filter.text.ToUpper());
										});
		
		foreach(GameObject g in nonMatched)
		{
			g.SetActiveRecursively(false);
		}
		
		entryHolder.repositionNow = true;
	}
	
	public void NewButtonClicked()
	{
		if(target != null)
			target.SendMessage(newClickedFunction, this, SendMessageOptions.DontRequireReceiver);
	}
	
	public void OKButtonClicked()
	{
		if(target != null)
			target.SendMessage(okClickedFunction, this, SendMessageOptions.DontRequireReceiver);
		Destroy(gameObject);
	}
	
	public void entryToggled(BrowseEditEntry entry)
	{
		bool wasIn = toggledEntries.Contains(entry.entryFile);
		
		if(singleSelect)
		{
			toggledEntries.Clear();
		}
		
		if(entry.toggler.isChecked && !wasIn)
			toggledEntries.Add(entry.entryFile);
		else if(toggledEntries.Contains(entry.entryFile))
			toggledEntries.Remove(entry.entryFile);
	}
	
	public void infoButtonHit(BrowseEditEntry entry)
	{
		infoTarget = entry.name;
		
		if(target != null && !string.IsNullOrEmpty(infoButtonOverrideFunction))
			target.SendMessage(infoButtonOverrideFunction, this, SendMessageOptions.DontRequireReceiver);
	}
	
	public void RemoveInfoTarget()
	{
		GameObject iTarget = entryObjects.Find(o => o.name == infoTarget);
		
		entryObjects.Remove(iTarget);
		Destroy(iTarget);
		
		entryHolder.repositionNow = true;
	}
	
	public void renamedObject(KeyValuePair<string,string> objectInfo)
	{
		GameObject iTarget = entryObjects.Find(o => o.name == objectInfo.Key || o.name == objectInfo.Key + "$" || o.name == objectInfo.Key + "^" || o.name == objectInfo.Key + "@");
		
		BrowseEditEntry bee = iTarget.GetComponent<BrowseEditEntry>();
		bee.entryName = objectInfo.Value;
		bee.nameLabel.text = bee.entryName;
	}
}
